package com.game.springbootstart.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.springbootstart.annotation.AutoFill;
import com.game.springbootstart.config.QueryPageParam;
import com.game.springbootstart.config.Result;

import com.game.springbootstart.entity.Notices;
import com.game.springbootstart.enumeration.OperationType;
import com.game.springbootstart.service.INoticesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-24
 */
@RestController
@RequestMapping("/notices")
public class NoticesController {
    @Autowired
    private INoticesService noticesService;

    @GetMapping("/list")
    public Result list() {
        return Result.success(noticesService.list());
    }

    @AutoFill(OperationType.INSERT)
    @PostMapping("/save")
    public Result save(@RequestBody Notices notices) {
        return Result.success(noticesService.save(notices));
    }

    @AutoFill(OperationType.UPDATE)
    @PostMapping("/mod")
    public Result mod(@RequestBody Notices notices) {
        return Result.success(noticesService.updateById(notices));
    }

    @PostMapping("/saveOrMod")
    public Result saveOrMod(@RequestBody Notices notices) {
        return Result.success(noticesService.saveOrUpdate(notices));
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        return Result.success(noticesService.removeById(id));
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        Page<Notices> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Notices> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Notices::getName, name);
        IPage<Notices> result = noticesService.page(page, queryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @GetMapping("/view")
    public Result view(Integer id) {
        String notices=noticesService.viewNotices(id);
        return Result.success(notices);
    }

    @GetMapping("/viewNotices")
    public Result viewNotices() {
        List<Notices> notices = noticesService.listBy();
        return Result.success(notices);
    }
}
